<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <button class="portBtn">导出为TXT</button>
</body>
<script src="../js/jquery-3.2.1.min.js"></script>
<script>

    var str = '';
    $('.portBtn').on('click',function(){
        $.get('/get',function(data){
            console.log(data);
            for(var key in data[0]){
                str += key+'/'
            }
            str += ';\r\n';
            data.forEach(e => {
                for(var key in e){
                    str += e[key]+'/';
                }
                str += ';\r\n';
            });
            funDownload(str,'1.txt')
        })
    })
    
    var funDownload = function (content, filename) {
        // 创建隐藏的可下载链接
        var eleLink = document.createElement('a');
        eleLink.download = filename;
        eleLink.style.display = 'none';
        // 字符内容转变成blob地址
        var blob = new Blob([content]);
        eleLink.href = URL.createObjectURL(blob);
        // 触发点击
        document.body.appendChild(eleLink);
        eleLink.click();
        // 然后移除
        document.body.removeChild(eleLink);
    };
    
</script>
</html>